#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/1/10 0010 15:19
# @Author : Ares_Wang
# @Site : 
# @File : classify.py
# @Software: PyCharm

from libsvm.python.svmutil import *
from src.core import *
from src.utils import *
from libsvm.tools.grid import *
import os
import numpy as np

def analysis(feature_folder, method, algorithms):
    trained_feature_folder = os.path.join(feature_folder, method, "train")
    tested_feature_folder = os.path.join(feature_folder, method, "test")
    optimized_feature_folder = os.path.join(feature_folder, method, "optimize")

    data = Data()
    ft = Feature()

    trained_txt_paths = []
    data.find_inside_file(trained_txt_paths, trained_feature_folder)
    tested_txt_paths = []
    data.find_inside_file(tested_txt_paths, tested_feature_folder)

    all_trained_x = []
    all_trained_y = []
    for trained_txt_path in trained_txt_paths:
        y, x = svm_read_problem(trained_txt_path)  # 读入训练文件
        # 将几个文件的特征合在一起
        all_trained_x += x
        all_trained_y += y

    # 参数寻优
    np_all_trained_y = np.array(all_trained_y)
    np_all_trained_x = trans_features2numpy(all_trained_x)
    ft.save_integrated_txt(np_all_trained_x, np_all_trained_y, os.path.join(optimized_feature_folder, "train_all.txt"))  # 整合训练文件用以寻优
    best_rate, best_param = find_parameters(os.path.join(optimized_feature_folder, "train_all.txt"))
    param_list = list(best_param.values())

    for alg_name in algorithms:
        alg = getattr(classification, alg_name.upper())
        alg_object = alg()
        if alg_name.upper() == "SVM":
            options = '-c ' + str(param_list[0]) + ' ' + '-g ' + str(param_list[1]) + ' ' + '-b 0'
            model = alg_object.train_model(all_trained_y, all_trained_x, options=options)
            print('******************SVM预测:开始********************')
            for tested_txt_path in tested_txt_paths:
                tested_y, tested_x = alg_object.read_feature(tested_txt_path)
                labels, acc, vals = alg_object.predict(tested_y, tested_x, model)
            print('******************SVM预测:结束********************')
        elif alg_name.upper() == "LP":
            gamma = param_list[1]
            model = alg_object.train_model(np_all_trained_y, np_all_trained_x, max_iter=1000, gamma=gamma)
            print('******************LP预测:开始********************')
            for tested_txt_path in tested_txt_paths:
                tested_y, tested_x = alg_object.read_feature(tested_txt_path)
                np_tested_y = np.array(tested_y)
                np_tested_x = trans_features2numpy(tested_x)
                predicted_result, accuracy = alg_object.predict(np_tested_y, np_tested_x, model)
                print('accuracy:%f' % accuracy)
            print('******************LP预测:结束********************')
        else:
            raise ValueError("算法输入错误")

algorithms = ["SVM", "LP"]
feature_folder = r'H:\wangjianlian\project\Python\image-pretreatment\resources\features'
method = 'HOG'

analysis(feature_folder, method, algorithms)
